package com.huanf.media.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.huanf.domain.dto.QueryMediaParamsDto;
import com.huanf.domain.dto.UploadFileParamsDto;
import com.huanf.domain.dto.UploadFileResultDto;
import com.huanf.domain.entity.MediaFiles;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.base.model.RestResponse;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 媒资信息(MediaFiles)表服务接口
 *
 * @author makejava
 * @since 2024-04-15 22:18:03
 */
public interface MediaFilesService extends IService<MediaFiles> {

    //根据媒资id查询文件信息
    MediaFiles getFileById(String mediaId);

    /**
     * @description 媒资文件查询方法
     * @param pageParams 分页参数
     * @param queryMediaParamsDto 查询条件
     * @return com.xuecheng.base.model.PageResult<com.xuecheng.media.model.po.MediaFiles>
     */
    PageResult<MediaFiles> queryMediaFiels(Long companyId, PageParams pageParams, QueryMediaParamsDto queryMediaParamsDto);

    /**
     * 上传文件
     * @param companyId 机构id
     * @param uploadFileParamsDto 文件信息
     * @param localFilePath 文件本地路径
     * @return
     */
    public UploadFileResultDto uploadFile(Long companyId, UploadFileParamsDto uploadFileParamsDto,String localFilePath, String objectName);

    MediaFiles addMediaFilesToDb(Long companyId, String md5, UploadFileParamsDto uploadFileParamsDto, String bucketMediafiles, String objectName);

    /**
     * 检查文件是否存在
     *
     * @param fileMd5 文件的md5
     * @return
     */
    RestResponse<Boolean> checkFile(String fileMd5) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException;

    /**
     * 检查分块是否存在
     * @param fileMd5       文件的MD5
     * @param chunkIndex    分块序号
     * @return
     */
    RestResponse<Boolean> checkChunk(String fileMd5, int chunkIndex);

    /**
     * 上传分块
     * @param fileMd5   文件MD5
     * @param chunk     分块序号
     * @param bytes     文件字节
     * @return
     */
    RestResponse uploadChunk(String fileMd5, int chunk,String localChunkPath);

    public RestResponse mergeChunks(Long companyId, String fileMd5, int chunkTotal, UploadFileParamsDto uploadFileParamsDto);

    public void addWaitingTask(MediaFiles mediaFiles);

    public File downloadFileFromMinIO(String bucket, String objectName);

    public boolean addMediaFilesTOMinIo(String objectName, String mimeType, String localFilePath, String bucket);
}
